/******************************************************
 * hello world program with menu and dialog resources *
 ******************************************************/

#include <windows.h>
#include "resource.h"

static HWND hWndMain;
static HBITMAP hBmp;
static HINSTANCE hInst;
static int hBmpDx,hBmpDy;
static char szWndClassName[] = "CMyProg" ;

int CALLBACK AboutDlgProc (HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
   switch (message)
   {
      case WM_INITDIALOG:            /* Dialog initialisieren */
         return 1;

      case WM_COMMAND:
         if (wParam==IDOK ||         /* "OK" selektiert? */
             wParam==IDCANCEL)       /* ESCAPE gedrueckt? */
         {
             EndDialog(hDlg,0);       /* Dialog schliessen */
             return 1;
         }
         break;

      case WM_DESTROY:
         EndDialog(hDlg,0);
         return 1;
    }
    return 0;                        /* Message nicht bearbeitet */
}

void About (HWND hWnd)
{
   DialogBox(hInst,MAKEINTRESOURCE(IDD_ABOUT),hWnd,AboutDlgProc);
}

LRESULT CALLBACK WndProc (HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
   switch (message)
   {
      case WM_PAINT:
      {
         HDC hdc;
         PAINTSTRUCT ps;

         hdc = BeginPaint(hWnd,&ps);

         if (hBmp) {
            HDC memdc;
            HBITMAP oldbmp;

            memdc = CreateCompatibleDC(hdc);
            oldbmp = SelectObject(memdc,hBmp);
            BitBlt(hdc,0,0,hBmpDx,hBmpDy,memdc,0,0,SRCCOPY);
            SelectObject(memdc,oldbmp);
            DeleteDC(memdc);
         }
         else
            TextOut(hdc,0,0,"No picture!",11);

         EndPaint(hWnd,&ps);
         return 0;
      }

      case WM_COMMAND:

         switch (wParam) {
         case IDM_GAMENEW:   MessageBeep(0); break;
         case IDM_GAMEEXIT:  PostQuitMessage(0); break;
         case IDM_HELPABOUT: About(hWnd); break;
         } 
         break;

      case WM_DESTROY:
      {
         PostQuitMessage(0);
         return 0;
      }
   }
   return DefWindowProc(hWnd,message,wParam,lParam);
}

int InitApplication (HINSTANCE hInstCurr)
{
   WNDCLASS wc;   /* window class */

   wc.style         = CS_HREDRAW | CS_VREDRAW;
   wc.lpfnWndProc   = (WNDPROC)WndProc;
   wc.cbClsExtra    = 0;
   wc.cbWndExtra    = 0;
   wc.hInstance     = hInstCurr;
   wc.hIcon         = LoadIcon(hInstCurr,MAKEINTRESOURCE(IDI_MAINICON));
   wc.hCursor       = LoadCursor(0,IDC_ARROW);
   wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
   wc.lpszMenuName  = MAKEINTRESOURCE(IDM_MAINMENU);
   wc.lpszClassName = szWndClassName;
   return RegisterClass(&wc);
}

int InitInstance (HINSTANCE hInstCurr,int nCmdShow)
{
   hWndMain = CreateWindow(szWndClassName,
      "Hello World",
      WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT,
      CW_USEDEFAULT,
      hBmpDx+2*GetSystemMetrics(SM_CXDLGFRAME),
      hBmpDy+GetSystemMetrics(SM_CYCAPTION)+GetSystemMetrics(SM_CYMENU)+2*GetSystemMetrics(SM_CYDLGFRAME),
      NULL,
      NULL,
      hInstCurr,
      NULL);

   if (hWndMain)
   {
      ShowWindow(hWndMain, nCmdShow);
      UpdateWindow(hWndMain);
      return (TRUE);
   }
   return FALSE;
}

int WinMain (HINSTANCE hInstCurr,HINSTANCE hInstPrev,LPSTR lpszCmd,int nCmdShow)
{
   MSG msg;
   HACCEL hAccel;
   BITMAP bmp;

   hInst = hInstCurr;
   hAccel = LoadAccelerators(hInst,MAKEINTRESOURCE(IDA_SHORTCUTS));
   if (!hAccel) return 0;

   /* load bitmap and calculate its size */
   hBmp = LoadBitmap(hInst,MAKEINTRESOURCE(IDB_BMP));
   if (hBmp) {
      GetObject(hBmp,sizeof(BITMAP),&bmp);
      hBmpDx = bmp.bmWidth;
      hBmpDy = bmp.bmHeight;
   }

   if (!hInstPrev)
      if (!InitApplication(hInstCurr)) return 0;

   if (!InitInstance(hInstCurr,nCmdShow)) return 0;

   while (GetMessage(&msg,NULL,0,0))
   {
      if (!TranslateAccelerator(hWndMain,hAccel,&msg)) {
         TranslateMessage(&msg);
         DispatchMessage(&msg);
      }
   }

   return msg.wParam ;
}
